//////////////////////////////////////////////////////////////////////////
//
// GAM203
// Sound Sample
// SoundSample.cpp
// 
// Scott Taylor
// Qantm College
//
//////////////////////////////////////////////////////////////////////////

#include "SoundSample.h"

#include <fmod.h>

#define WIN32_LEAN_AND_MEAN
#include <windows.h>

SoundSample::SoundSample(const char* a_pFilename, bool a_is3d)
: m_pSample(NULL)
, m_errorCode(FMOD_ERR_NONE)
{
	if (a_pFilename)
	{
		// Copy over filename
		strcpy_s(m_filename, a_pFilename);

		// Load sound with support for 3D positioning (software)
		// We could request hardware support here by passing FSOUND_HW3D for mode
		// We could optimise out 3D position calculations by passing FSOUND_2D for mode
		// (This could be good for GUI sounds or music)
		m_pSample = FSOUND_Sample_Load(FSOUND_FREE, a_pFilename, a_is3d ? FSOUND_HW3D : FSOUND_2D, 0, 0);

		// Some advanced settings
		if (m_pSample)
		{
			// Set up distance attenuation
			FSOUND_Sample_SetMinMaxDistance(m_pSample, 1.0f, 100.0f);

			// Set up automatic frequency variation
			FSOUND_Sample_SetDefaultsEx(m_pSample, -1, -1, FSOUND_STEREOPAN, -1, a_is3d ? 100 : 0, -1, -1);
		}
		else
		{
			// Failed to load sample
			m_errorCode = FSOUND_GetError();

			OutputDebugString("Failed to load sound (");
			switch (m_errorCode)
			{
			case FMOD_ERR_UNINITIALIZED:
				OutputDebugString("FMOD not initialised");
				break;
			case FMOD_ERR_CREATEBUFFER:
				OutputDebugString("error creating hardware buffer");
				break;
			case FMOD_ERR_FILE_NOTFOUND:
				OutputDebugString("file not found");
				break;
			case FMOD_ERR_FILE_FORMAT:
			case FMOD_ERR_FILE_BAD:
			case FMOD_ERR_VERSION:
				OutputDebugString("could not read file");
				break;
			default:
				OutputDebugString("other error");
				break;
			}
			OutputDebugString(")...\n  Sound file: [");
			OutputDebugString(a_pFilename);
			OutputDebugString("]\n");
		}
	}
	else
	{
		m_filename[0] = 0;
	}
}

SoundSample::~SoundSample()
{
	if (m_pSample)
	{
		FSOUND_Sample_Free(m_pSample);
		m_pSample = NULL;
	}
}

